//Base Station Configration

#include<p18f4550.h>

#include <delays.h>
#include <stdlib.h>
#include <usart.h> 


#pragma config FOSC = INTOSC_HS
#pragma config WDT = OFF
#pragma config DEBUG = ON
#pragma config LVP = OFF

// HT12D Vs PIC Mapping

#define   R_Data0       PORTBbits.RB0  
#define   R_Data1       PORTBbits.RB1  
#define   R_Data2       PORTBbits.RB2  
#define   R_Data3       PORTBbits.RB3  


#define   R_AD0         PORTBbits.RB4  
#define   R_AD1         PORTBbits.RB5  
#define   R_AD2         PORTBbits.RB6  
#define   R_AD3         PORTBbits.RB7 

#define   V_tst         PORTCbits.RC5

//HT12E Vs PIC Mapping

#define   T_Data0       PORTDbits.RD0  
#define   T_Data1       PORTDbits.RD1  
#define   T_Data2       PORTDbits.RD2  
#define   T_Data3       PORTDbits.RD3  


#define   T_AD0         PORTDbits.RD4  
#define   T_AD1         PORTDbits.RD5  
#define   T_AD2         PORTDbits.RD6  
#define   T_AD3         PORTDbits.RD7 

#define   TE            PORTCbits.RC4


void run(int x);
void sleep(int y);


char byte,byte2;
char UVal;
char RUVal;
char WUVal;




void main(void)
{

   

	// Ports initialization 
    PORTA=0x00; 
	PORTB=0x00; 
	PORTC=0x00; 
	PORTD=0x00; 
	PORTE=0x00; 
	
	LATA=0x00; 
	LATB=0x0F; 
	LATC=0x00; 
	LATD=0x00; 
	LATE=0x00; 
	
	TRISA=(0b00000000);
	TRISB=(0b00001111);
	TRISC=(0b00000000);
	TRISD=(0b00000000);
	TRISE=(0b00000000);

    TE=1;

     // Set Nodes to Sleep Mode 
        // set address of N1
        T_AD0 = 1;
        T_AD1 = 0;
        T_AD2 = 0;
        T_AD3 = 0;

        // write sleep command word to N1
        T_Data0 =0;
 		T_Data1 =0;
		T_Data2 =0;
        T_Data3 =1;

        //  Transmmit command to N1 by setting TE = 0

        TE = 0;
       // wait for complete transsmission 20 miro second
       Delay10TCYx(100);
   
      // reset TE
        TE = 1;
      // set address of N2
        T_AD0 = 0;
        T_AD1 = 1;
        T_AD2 = 0;
        T_AD3 = 0;
       
      // write sleep command word to N2
       
        T_Data0 =0;
 		T_Data1 =0;
		T_Data2 =0;
        T_Data3 =1;

		//  Transmmit command to N1 by setting TE = 0

        TE = 0;
	   
        //  wait for complete transsmission 20 miro second
       Delay10TCYx(100);
      // reset TE
        TE = 1;


     
 // usart configration

	OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF
			  & USART_ASYNCH_MODE & USART_EIGHT_BIT 
			  & USART_CONT_RX & USART_BRGH_HIGH,25);       
      
while(1)
{
  
     while(BusyUSART());    //if usart busy do nothing
       
     UVal =ReadUSART();     //read value from usart
     
     if (UVal == 0x20)  {sleep(1);}   // call N1 sleep routin   set x=1 ,call sleep(x)
     if (UVal == 0xA0)  {run(1);}    //  call N1 run   routin   //  x=1  ,call run(x)
     if (UVal == 0x40)  {sleep(2);}  //  call N2 sleep routin  //   x=2  ,call sleep(x)
     if (UVal == 0xC0)  {run(2);}  //    call N2 run   routin  //   x=2  ,call run(x)
    
 }//End while(1)

}//end main

void run(int x)
   {
 int i;
// set node number to -->x[1,2]
    if(x==1)
    {
       // set address on tr to node 1
        T_AD0 = 1;
        T_AD1 = 0;
        T_AD2 = 0;
        T_AD3 = 0;
       //write run command on TR data port
        T_Data0 =0;
 	   	T_Data1 =0;
		T_Data2 =0;
        T_Data3 =0;
       // transmitt
        TE=0;
       // wait for complet transmission 20 micro seond
    Delay10TCYx(100);
       TE=1;
    
     // get Data From Node 1-------------------------------------

        R_AD0 = 1;
		R_AD1 = 0;
      //-----wait for reciption-----
      Delay10TCYx(100);
     //-----get TempLow,TempHigh,HumLow,HumHigh,Voltage
     
     for(i=0;i<4;i++)
       { 
            byte = PORTB;
   	     	byte = byte & 0x0F;
        while(BusyUSART()); 
              WriteUSART(byte); //write data To PC via Usart
       }//end for
 		   

     } // end if 1
     if(x==2) 
       {
         // set address on tr to node 2
        T_AD0 = 0;
        T_AD1 = 1;
        T_AD2 = 0;
        T_AD3 = 0;
        //write run command on TR data port
        T_Data0 =1;
 	   	T_Data1 =0;
		T_Data2 =0;
        T_Data3 =1;
        // transmitt
        TE=0;
        // wait for complet transmission 20 micro seond
        Delay10TCYx(100);
        TE=1;
      // get Data From Node 2-------------------------------------

        R_AD0 = 0;
		R_AD1 = 1;
      //-----wait for reciption-----
      Delay10TCYx(100);
     //-----get TempLow,TempHigh,HumLow,HumHigh,Voltage
     
     for(i=0;i<4;i++)
        { 
            byte = PORTB;
   	     	byte = byte & 0x0F;
        while(BusyUSART()); 
              WriteUSART(byte); //write data To PC via Usart
        }//end for
       
       } // end if 2
    
      }//end run 

void sleep(int y)
     {
      if(y == 1)
        {
         // set nod 1 address
           T_AD0 = 1;
           T_AD1 = 0;
           T_AD2 = 0;
           T_AD3 = 0;
         // write sleep command to data port
		    T_Data0 =1;
 	       	T_Data1 =0;
	    	T_Data2 =0;
            T_Data3 =0;
         TE = 0;
         // wait for 20 micro second

        Delay10TCYx(100);
         TE=1;

        }// end if n 1

     if(y == 2)
        {
         // set nod 2 address
           T_AD0 = 0;
           T_AD1 = 1;
           T_AD2 = 0;
           T_AD3 = 0;
         // write sleep command to data port
		    T_Data0 =1;
 	       	T_Data1 =0;
	    	T_Data2 =0;
            T_Data3 =0;
         TE = 0;
         // wait for 20 micro second

        Delay10TCYx(100);
         TE=1;

        }// end if n 2


     }// end sleep



